﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SEO.Business;
using System.Data;


/// <summary>
/// 新闻源分析
/// </summary>
public partial class NewsWebSEOList : AdminPage
{
    /// <summary>
    /// 用户ID
    /// </summary>
    protected int UserID
    {
        get { return ViewState["UserID"] == null ? 0 : Convert.ToInt32(ViewState["UserID"]); }
        set { ViewState["UserID"] = value; }
    }

    /// <summary>
    /// 文章标题
    /// </summary>
    protected string PageTitle
    {
        get { return ViewState["PageTitle"] == null ? "" : ViewState["PageTitle"].ToString(); }
        set { ViewState["PageTitle"] = value; }
    }

    /// <summary>
    /// 开始时间
    /// </summary>
    protected string BeginDateTime
    {
        get { return ViewState["BeginDateTime"] == null ? "" : ViewState["BeginDateTime"].ToString(); }
        set { ViewState["BeginDateTime"] = value; }
    }

    protected int nNum = 0;

    protected bool IsShowDelete = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            
            BindUser();
            InitData();
        }
    }

    /// <summary>
    /// 权限控制
    /// </summary>
    /// <returns></returns>
    public override long GetNeededFeatures()
    {
        return AdminFEATURE.NewsWebSEOList;
    }

    /// <summary>
    /// 绑定用户列表
    /// </summary>
    protected void BindUser()
    {
        if (nAdminFeatures == 1 || SupportFeatures(AdminFEATURE.NewsWebSEO))
        {
            UserID = 0;
            ddlUser.Enabled = true;
        }
        else
        {
            UserID = nAdminUserID;
            ddlUser.Enabled = false;
        }
        DataTable dtUser = new BisAdminInfo().GetAllAdminList();
        ddlUser.Items.Clear();
        ddlUser.Items.Add(new ListItem("所有用户", "0"));
        foreach (DataRow drUser in dtUser.Rows)
        {
            ddlUser.Items.Add(new ListItem(drUser["Name"].ToString(), drUser["ID"].ToString()));
        }
        ddlUser.SelectedValue = UserID.ToString();
        dtUser.Dispose();
    }

    /// <summary>
    /// 初始化数据
    /// </summary>
    protected void InitData()
    {
        //权限显示控制
        if (nAdminFeatures == 1 || SupportFeatures(AdminFEATURE.NewsWebSEO))
        {
            UserListPanel.Visible = true;
            AddPanel.Visible = true;
            DeleteTitle.Visible = true;
            IsShowDelete = true;
            btnClear.Visible = true;
            btnExcel.Visible = true;
        }
        else
        {
            UserListPanel.Visible = false;
            AddPanel.Visible = false;
            DeleteTitle.Visible = false;
            IsShowDelete = false;
            UserID = nAdminUserID;
            btnClear.Visible = false;
            btnExcel.Visible = false;
        }

        DateTime dBeginTime = Convert.ToDateTime("1753-01-01 12:00:00");
        DateTime dEndTime = Convert.ToDateTime("9999-12-31 11:59:59");
        if (!string.IsNullOrEmpty(BeginDateTime))
        {
            dBeginTime = Convert.ToDateTime(BeginDateTime);
            dEndTime = dBeginTime.AddDays(1);

        }
        int nRecordCount = 0;
        int nPageCount = 0;
        DataTable dtNewsWebSEO = new BisNewsWebSEO().GetList(UserID,PageTitle,dBeginTime,dEndTime, anpPage.CurrentPageIndex, anpPage.PageSize, ref nPageCount, ref nRecordCount);
        nNum = (anpPage.CurrentPageIndex - 1) * anpPage.PageSize;
        nNum++;
        anpPage.RecordCount = nRecordCount;
        if (nRecordCount == 0)
        {
            noData.Style["display"] = "block";
        }
        else
        {
            noData.Style["display"] = "none";
        }
        rptNewsWebSEO.DataSource = dtNewsWebSEO;
        rptNewsWebSEO.DataBind();
        dtNewsWebSEO.Dispose();

        anpPage.CustomInfoHTML = string.Format("<div class=\"pagetag\">当前第<b>{0}</b>/<b>{1}</b>页 共<b>{2}</b>条记录</div>", new object[] { anpPage.CurrentPageIndex, anpPage.PageCount, anpPage.RecordCount });
    }

    /// <summary>
    /// 列表绑定
    /// </summary>
    /// <param name="source"></param>
    /// <param name="e"></param>
    protected void rptNewsWebSEO_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            if (nAdminFeatures == 1 || SupportFeatures(AdminFEATURE.NewsWebSEO))
            {
                //删除信息
                bool result = new BisNewsWebSEO().Delete(Convert.ToInt32(e.CommandArgument));
                Until.Alert(result ? "删除成功！" : "删除失败！");
                InitData();
            }
        }
    }

    /// <summary>
    /// 分页事件
    /// </summary>
    /// <param name="src"></param>
    /// <param name="e"></param>
    protected void anpPage_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {
        anpPage.CurrentPageIndex = e.NewPageIndex;
        InitData();
    }

    /// <summary>
    /// 刷新页面
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnReset_Click(object sender, EventArgs e)
    {
        InitData();
    }

    /// <summary>
    /// 查询
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        UserID = Convert.ToInt32(ddlUser.SelectedValue);
        PageTitle = txtPageTitle.Text.Trim();
        BeginDateTime = txtDateTime.Text.Trim();
        anpPage.CurrentPageIndex = 1;
    }

    /// <summary>
    /// 导出
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnExcel_Click(object sender, EventArgs e)
    {
        this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "", "window.open('CreateExcel.aspx?type=userarticlereportlist&userid=" + UserID + "&pagetitle=" + PageTitle + "&begindatetime=" + BeginDateTime + "');", true);
    }

    /// <summary>
    /// 清空
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnClear_Click(object sender, EventArgs e)
    {
        //删除信息
        bool result = new BisNewsWebSEO().DeleteAll();
        Until.Alert(result ? "删除成功！" : "删除失败！");
        InitData();
    }
}